package com.hlf.modules.merchant.order.controller;

import com.hlf.common.utils.PageUtils;
import com.hlf.common.utils.ReturnResult;
import com.hlf.modules.merchant.order.service.OrderService;
import com.hlf.modules.merchant.user.entity.UserBind;
import com.hlf.modules.merchant.user.service.UserBindService;
import com.hlf.modules.sys.controller.AbstractController;
import com.hlf.modules.sys.service.SysUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 订单
 * Created by Administrator on 2017/9/22.
 */

@RestController
@RequestMapping("/order")
public class OrderController extends AbstractController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Autowired
    private UserBindService userBindService;

    /**
     * 订单列表
     */
    @RequestMapping("/list")
    public ReturnResult list(@RequestParam Map<String, Object> params) {

        Long userId = getUserId();

        //判断当前用户权限
        List<Long> roleIdList = sysUserRoleService.queryRoleIdList(userId);
        Long minRoleId = Collections.min(roleIdList, null);

        PageUtils pageUtil = null;

        if (minRoleId == 1) {
            //管理员
            pageUtil = orderService.queryListForPage(params);
        } else if (minRoleId == 2) {
            //商家
            List<UserBind> userBindList = userBindService.queryListForUserId(userId);
            UserBind userBind = userBindList.get(0);
            Long merchantId = userBind.getMerchantId();
            //
            pageUtil = orderService.queryListForMerchantId(params,merchantId);

        } else {
            //门店高管 & 点餐员
            List<UserBind> userBindList = userBindService.queryListForUserId(userId);
            UserBind userBind = userBindList.get(0);
            Long storeId = userBind.getStoreId();
            pageUtil = orderService.queryListForStoreId(params,storeId);

        }




        return ReturnResult.ok().put("page", pageUtil);
    }


}
